package com.example.allshengmedical.system.activity;

import java.text.SimpleDateFormat;
import java.util.Date;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.SystemClock;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.example.allshengmedical.R;
import com.example.allshengmedical.base.BaseActivity;
import com.example.allshengmedical.mainmenu.activity.MainMenuActivity;
import com.example.allshengmedical.system.presenter.HotMachinePresenter;
import com.example.allshengmedical.system.view.IHotMachineView;
import com.example.allshengmedical.util.ByAlert;
import com.itboye.allshengmedical.sci.DefaultSerialControl;
import com.itboye.allshengmedical.sci.ISerialControl;
import com.itboye.allshengmedical.sci.cmd.req.AreaCalibration1Cmd;
import com.itboye.allshengmedical.sci.cmd.req.CmdType;
import com.itboye.allshengmedical.sci.packet.resp.BaseRespPacket;

public class HotMachineActivity extends BaseActivity implements
		IHotMachineView, OnClickListener {
	HotMachinePresenter hotMachinePresenter;
	TextView up_btn, down_btn, left_btn, right_btn;
	EditText up_edit, down_edit, left_edit, right_edit;
	ISerialControl controlInstance;
	TextView calibra_btn;
	EditText lv_edit;

	@Override
	protected int setcontent() {
		// TODO Auto-generated method stub
		return R.layout.activity_hot_machine;
	}

	@Override
	protected void initView() {
		// TODO Auto-generated method stub
		controlInstance = DefaultSerialControl.getInstance();
		controlInstance.init();
		controlInstance.start();
		controlInstance.setHandler(handler);

	}

	private Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
			case ISerialControl.FindPacketMessage:
				updateMsg(msg.getData());
				break;
			case ISerialControl.SCTimeoutMessage:
				break;
			}
		}
	};

	private void updateMsg(Bundle msg) {
		// TODO Auto-generated method stub
		String cmdType = msg.getString("cmdType");
		BaseRespPacket packet = (BaseRespPacket) msg.getSerializable("packet");
		if (cmdType.length() < 1)
			return;
		// 根据type 转换packet 为你想要的
		CmdType type = CmdType.getCmdType(cmdType.substring(1));
		// 发送指令后这里会收到数据
		Log.d("read_data", "接受到的响应数据包是" + cmdType + type.toDesc());
		switch (type) {
		case AreaCalibration:
			break;
		}
	}

	@Override
	protected void initData() {
		// TODO Auto-generated method stub
		hotMachinePresenter = new HotMachinePresenter(this);
		hotMachinePresenter.initAppHead();
		NumberFormat format = new NumberFormat();
		format.Format(up_edit);
		format.Format(down_edit);
		format.Format(left_edit);
		format.Format(right_edit);
		format.Format(lv_edit);

	}

	@Override
	public void initAppHead() {
		// TODO Auto-generated method stub
		setSystemTime();
		setActionBar();
	}

	private void setActionBar() {
		// TODO Auto-generated method stub
		Button leftfirstBtn;
		Button leftsecond_btn;
		TextView title;
		leftfirstBtn = BindView(R.id.leftfirst_btn);
		leftsecond_btn = BindView(R.id.leftsecond_btn);
		title = BindView(R.id.title);
		leftfirstBtn.setVisibility(View.VISIBLE);
		leftsecond_btn.setVisibility(View.VISIBLE);
		leftsecond_btn.setText(R.string.item_actionbar_home);
		leftfirstBtn.setText(R.string.item_actionbar_back);
		title.setText("热机设置");
		leftsecond_btn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(HotMachineActivity.this,
						MainMenuActivity.class);
				startActivity(intent);
				finish();
			}
		});
		leftfirstBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(HotMachineActivity.this,
						OpticalActivity.class);
				startActivity(intent);
				finish();
			}
		});
	}

	private void setSystemTime() {
		// TODO Auto-generated method stub
		TextView date, fileName;
		date = BindView(R.id.date);
		fileName = BindView(R.id.file_name);

		// 获取系统时间
		SimpleDateFormat sdfDate = new SimpleDateFormat("MM dd,yyyy");
		date.setText(sdfDate.format(new Date()));

		// 文件路径置空
		fileName.setText("");

	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		controlInstance.addCommand(CmdType.EndAreaCalibration, null, 0);
		SystemClock.sleep(1000);
		controlInstance.pause();
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		NumberFormat format = new NumberFormat();
		switch (v.getId()) {

		case R.id.up_btn: {
			int up = 0;
			String string = up_edit.getText() + "";
			if (up_edit.getText() != null
					&& !"".equals(string)
					&& Double.parseDouble(up_edit.getText().toString()) >= -10.0
					&& Double.parseDouble(up_edit.getText().toString()) <= 10) {
				up = format.translateNum(up_edit.getText().toString());
				controlInstance.addCommand(CmdType.AreaCalibration, "02", up);
			} else {
				ByAlert.alert("输入参数非法");
			}
		}
			break;
		case R.id.down_btn: {
			int up = 0;
			String string = down_edit.getText() + "";
			if (down_edit.getText() != null
					&& !"".equals(string)
					&& Double.parseDouble(down_edit.getText().toString()) >= -10.0
					&& Double.parseDouble(down_edit.getText().toString()) <= 10) {
				up = format.translateNum(down_edit.getText().toString());
				controlInstance.addCommand(CmdType.AreaCalibration, "03", up);
			} else {
				ByAlert.alert("输入参数非法");
			}
		}
			break;
		case R.id.left_btn: {
			int up = 0;
			String string = left_edit.getText() + "";
			if (left_edit.getText() != null
					&& !"".equals(string)
					&& Double.parseDouble(left_edit.getText().toString()) >= -10.0
					&& Double.parseDouble(left_edit.getText().toString()) <= 10) {
				up = format.translateNum(left_edit.getText().toString());
				controlInstance.addCommand(CmdType.AreaCalibration, "04", up);
			} else {
				ByAlert.alert("输入参数非法");
			}
		}
			break;
		case R.id.right_btn: {
			int up = 0;
			String string = right_edit.getText() + "";
			if (right_edit.getText() != null && !"".equals(string)
					&& Double.parseDouble(right_edit.getText() + "") >= -10.0
					&& Double.parseDouble(right_edit.getText() + "") <= 10) {
				up = format.translateNum(right_edit.getText() + "");
				controlInstance.addCommand(CmdType.AreaCalibration, "05", up);
			} else {
				ByAlert.alert("输入参数非法");
			}
		}
			break;
		case R.id.calibra_btn: {
			// TODO Auto-generated method stub
			int up = 0;
			String string = lv_edit.getText() + "";
			if (lv_edit.getText() != null && !"".equals(string)
					&& Double.parseDouble(lv_edit.getText() + "") >= -10.0
					&& Double.parseDouble(lv_edit.getText() + "") <= 10) {
				up = format.translateNum(lv_edit.getText() + "");
				controlInstance.addCommand(CmdType.AreaCalibration, "01", up);
			} else {
				ByAlert.alert("输入参数非法");
			}
		}
		default:
			break;
		}
	}

}
